package com.ajx.todo.study.architecturecomponent;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;


import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by lai on 2017/9/1.
 */

public class UserRepository {
    private WebService webService;
    final MutableLiveData<User> data=new MutableLiveData<>();
    public UserRepository(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://git.oschina.net")
                .build();

        webService = retrofit.create(WebService.class);
    }
    public LiveData<User> getUser(){

        webService.getUser().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    data.setValue(new Gson().fromJson(string,User.class));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        return data;
    }
}
